package cn.git.controller;

import cn.git.entity.Person;
import cn.git.service.CacheService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 缓存测试controller
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-09-23
 */
@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    private CacheService cacheService;

    /**
     * 根据id获取person
     * @param id id
     */
    @GetMapping("/person/{id}")
    public String getPersonById(@PathVariable("id") String id) {
        return JSONObject.toJSONString(cacheService.getPersonById(id));
    }

    /**
     * 根据id获取person,缓存自定义key
     *
     * @param id id
     */
    @GetMapping("/person/cus/{id}")
    public String getPerson(@PathVariable("id") String id) {
        return JSONObject.toJSONString(cacheService.getPersonByCusId(id));
    }

    /**
     * 更新person
     * @return
     */
    @GetMapping("/update/{id}")
    public String updatePerson(@PathVariable("id") String id) {
        Person person = new Person(id, "张三", 18, "男");
        return JSONObject.toJSONString(cacheService.updatePerson(person));
    }


    /**
     * 根据id删除person
     * @param id id
     */
    @GetMapping("/delete/{id}")
    public void deletePersonById(@PathVariable("id") String id) {
        cacheService.deletePersonById(id);
    }
}
